package com.itheima.demo03functionalinterface;

import com.sun.xml.internal.fastinfoset.util.StringArray;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author chenming
 * Date  2023/6/13 14:35
 * @version 1.0
 * description
 */
public class Demo04ConsumerAndThen {
//    方法定义
//    public static void main(String[] args) {
//        print(
//                str->{
//                    System.out.println(str.toUpperCase());
//                },
//                str->{
//                    System.out.println(str.toLowerCase());
//                }
//        );
//    }
//
//    public static void print(Consumer<String> s1,Consumer<String> s2){
//        s1.accept("abc");
//        s2.accept("CAB");
//        s1.andThen(s2);
//    }

    //应用场景：自生自销
    //格式化打印信息
    // 请按照格式“ 姓名：XX。性别：XX。 ”的格式将信息打印出来
    public static void main(String[] args) {
        //两种方式都是一样的
        formattorPersonMsg(
                (s1)->{
                    for (int i = 0; i < s1.length; i++) {
                        System.out.print(s1[i].split(",")[0] + " ");
                    }
                },
                (s2)->{
                    System.out.println();
                    for (int i = 0; i < s2.length; i++) {
                        System.out.print(s2[i].split(",")[1] + " ");
                    }
                }
        );

        printInfo(s->System.out.print("姓名:"+s.split(",")[0] +" "),
                s->System.out.println("性别:"+s.split(",")[1]),new String[] { "迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男" });
    }

    //先格式化数据
    public static void formattorPersonMsg(Consumer<String[]> con1, Consumer<String[]> con2) {
        // con1.accept(new String[]{ "迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男" });
        // con2.accept(new String[]{ "迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男" });
        // 一句代码搞定,相当于con1先accept，con2再accept
        con1.andThen(con2).accept(new String[] { "迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男" });
    }

    //2.打印数据
    public static void printInfo(Consumer<String> one, Consumer<String> two, String[] arr){
        Arrays.stream(arr).forEach(
                str ->{
                    one.andThen(two).accept(str);
                }
        );
    }



}
